<?

class helper_pager
{
	public static $per_page = 25;

	public static function url( $page, $url = null )
	{
		$url = preg_replace('/&?page=[0-9]+/', '', $url ? $url : $_SERVER['REQUEST_URI']);
		return $url . ( strpos($url, '?') ? '&' : '?' ) . 'page=' . $page;
	}

	public static function page()
	{
		return max(1, request::get_int('page'));
	}

	public static function next()
	{
		return self::page() + 1;
	}

	public static function prev()
	{
		return self::page() - 1;
	}

	public static function last_page( $total )
	{
		return ceil($total/self::$per_page);
	}

	public static function pages( $total, $num = 5 )
	{
		$num--;
		$delta = floor($num/2);

		$start = self::page() - $delta;
		if ( $start < 1 ) $start = 1;
		
		$end = $start + $num;
		if ( $end > self::last_page($total) ) $end = self::last_page($total);

		$pages = array();
		for ( $i = $start; $i <= $end; $i++ )
		{
			$pages[] = $i;
		}

		return $pages;
	}

	public static function offset()
	{
		return (self::page() - 1) * self::$per_page;
	}
}